import { cloneDeep } from 'lodash-es';
import { MODEL_NAME } from '../../constants/model-names';
import { EVENTS, type IEventBus } from '../../events';
import { BaseModel } from '../base';
import {
  PlayerProgressSaveManager,
  type PlayerProgressSnapshot,
  type PlayerProgressSnapshotOld,
} from './player-progress-save-manager';
import { PlayerProgressReader } from './player-progress-reader';

export interface PlayerProgressState {
  realm: number;
  initedSpiritualRoot: boolean;
  studyCultivationSkills: string[];
  killEnemies: Record<string, number>;
  createdAt: number;
}

export class PlayerProgressModel extends BaseModel {
  public name = MODEL_NAME.PLAYER_PROGRESS;
  private bus: IEventBus;
  private saveManager: PlayerProgressSaveManager;
  private progress: PlayerProgressState = {
    realm: 0,
    initedSpiritualRoot: false,
    studyCultivationSkills: [],
    /** 敌人击杀数 */
    killEnemies: {},
    /** 创建时间 */
    createdAt: 0,
  };

  constructor(bus: IEventBus) {
    super();
    this.bus = bus;
    this.saveManager = new PlayerProgressSaveManager();
  }

  public setRealm(realm: number) {
    this.progress.realm = realm;
    this.bus.emit(EVENTS.PLAYER_PROGRESS_STATE_UPDATE);
  }

  public setInitedSpiritualRoot(initedSpiritualRoot: boolean) {
    this.progress.initedSpiritualRoot = initedSpiritualRoot;
    this.bus.emit(EVENTS.PLAYER_PROGRESS_STATE_UPDATE);
  }

  public setLearnedCultivationSkills(studyCultivationSkills: string[]) {
    this.progress.studyCultivationSkills = studyCultivationSkills;
    this.bus.emit(EVENTS.PLAYER_PROGRESS_STATE_UPDATE);
  }

  public addLearnedCultivationSkill(id: string) {
    this.progress.studyCultivationSkills.push(id);
    this.bus.emit(EVENTS.PLAYER_PROGRESS_STATE_UPDATE);
  }

  /** 更新击杀敌人数 */
  private updateKillEnemies(enemyId: string) {
    this.progress.killEnemies[enemyId] =
      (this.progress.killEnemies[enemyId] || 0) + 1;
  }

  public updateKillEnemiesByIds(enemyIds: string[]) {
    for (const id of enemyIds) {
      this.updateKillEnemies(id);
    }
    this.bus.emit(EVENTS.PLAYER_PROGRESS_STATE_UPDATE);
  }

  public setCreatedAt(createdAt: number) {
    if (this.progress.createdAt > 0) return;
    this.progress.createdAt = createdAt;
  }

  public get realm() {
    return this.progress.realm;
  }

  public get initedSpiritualRoot() {
    return this.progress.initedSpiritualRoot;
  }

  public get studyCultivationSkills() {
    return this.progress.studyCultivationSkills;
  }

  public getState() {
    return cloneDeep(this.progress);
  }

  public getLifeYears() {
    return PlayerProgressReader.convertTimeAdvanced(this.progress.createdAt);
  }

  public get uiState() {
    return {
      realm: this.realm,
      initedSpiritualRoot: this.initedSpiritualRoot,
      state: this.getState(),
    };
  }

  public migrate(old: PlayerProgressSnapshotOld): PlayerProgressSnapshot {
    return this.saveManager.migrate(old);
  }

  public save(): PlayerProgressSnapshot {
    return this.saveManager.save(this.progress);
  }

  public load(snapshot: PlayerProgressSnapshot) {
    this.saveManager.load(snapshot, this.progress);
  }
}
